<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="__LAYUIMINI__/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="__LAYUIMINI__/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <link rel="stylesheet" href="__LAYUIMINI__/css/public.css" media="all">
    <style>
        .layui-upload-img{width: 92px; height: 92px;}
    </style>
</head>

<body>
    <div class="layuimini-container">
        <div class="layuimini-main">
            <!-- layui-form-pane 方框式表单 -->
            <form class="layui-form layui-form-pane" action="" lay-filter="example">

                <div class="layui-form-item">
                    <label class="layui-form-label">图片</label>
                    <div class="layui-input-block">
                        <div class="layui-upload">
                            <button type="button" class="layui-btn" id="test1">上传图片</button>
                            <div class="layui-upload-list">
                                <img class="layui-upload-img" id="demo1">
                                <p id="demoText"></p>
                            </div>
                            <input type="hidden" name="file_id" id="file_id">
                            <!-- <div style="width: 95px;">
                                <div class="layui-progress layui-progress-big" lay-showpercent="yes" lay-filter="demo">
                                    <div class="layui-progress-bar" lay-percent=""></div>
                                </div>
                            </div> -->
                        </div>
                        <!-- <a name="list-progress"> </a> -->
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">分类</label>
                    <div class="layui-input-block">
                        <select name="cate_id"  lay-search="">
                            <option value="0">请选择分类</option>
                            {volist name="$cateList" id="vo"}
                            <option value="{$vo.id}" >{$vo.cate_name}</option>
                            {/volist}
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">问卷</label>
                    <div class="layui-input-block">
                        <select name="naire_id"  lay-search="">
                            <option value="0">请选择问卷</option>
                            {volist name="$naireList" id="vo"}
                            <option value="{$vo.id}" >{$vo.title}</option>
                            {/volist}
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">排序</label>
                    <div class="layui-input-block">
                        <input type="number" name="sort" value="0" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-block">
                        <input type="checkbox" checked="" name="status" lay-skin="switch" lay-filter="switchTest"
                            lay-text="正常|关闭">
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <script src="__LAYUIMINI__/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script src="__LAYUIMINI__/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
    <script>
        layui.use(['form', 'upload', 'element'], function () {
            var form = layui.form,
                $ = layui.$,
                layer = layui.layer,
                upload = layui.upload,
                element = layui.element;



            //常规使用 - 普通图片上传
            var uploadInst = upload.render({
                elem: '#test1'
                , url: '/Upload/uploadImg' //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
                ,data:{
                    type:1
                }
                , before: function (obj) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                        $('#demo1').attr('src', result); //图片链接（base64）
                    });

                    element.progress('demo', '0%'); //进度条复位
                    layer.msg('上传中', { icon: 16, time: 0 });
                }
                , done: function (res) {
                    //如果上传失败
                    
                    if (res.status == 1) {
                        console.log(res)
                        $('#file_id').val(res.file_id)
                    }
                    layer.msg(res.msg);
                    //上传成功的一些操作
                    //……
                  
                    $('#demoText').html(''); //置空上传失败的状态
                }
                , error: function () {
                    //演示失败状态，并实现重传
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function () {
                        uploadInst.upload();
                    });
                }
                //进度条
                , progress: function (n, elem, e) {
                    element.progress('demo', n + '%'); //可配合 layui 进度条元素使用
                    if (n == 100) {
                        layer.msg('上传完毕', { icon: 1 });
                    }
                }
            });






            //监听指定开关
            form.on('switch(switchTest)', function (data) {

            });

            //监听提交
            form.on('submit(demo1)', function (data) {
                var data = data.field
                $.ajax({
                    type: "post",
                    url: "saveBanner",
                    data: data,
                    dataType: "json",
                    success: function (res) {
                        if (res.code == 1) {
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            // parent.layer.close(index); //再执行关闭 
                            // 关闭后调用
                            layer.msg(res.msg)
                            setTimeout(function () {
                                parent.layer.close(index);
                            }, 500)
                        } else {
                            layer.msg(res.msg)
                        }


                    }
                });
                return false;
            });


        });
    </script>

</body>

</html>